/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package trial.restful;
import trial.util.itemSet;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import trial.request.Request;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import trial.entity.Customer;
import trial.entity.Item;
import trial.util.customerSet;


/**
 *
 * @author feifei
 */
@Stateless
@Path("product")
public class RSbean {
     @EJB
    private Request request;
    /** Creates a new instance of managedBean */

    public RSbean() {
        
    }
    @POST
    @Path("getAllCurrentItems")
    @Consumes("application/xml")
    @Produces("application/xml")
    public itemSet getAllCurrentItems()
    {
        System.out.println("***************    RSbeantest");
        List<Item> item=request.showAllItems();
           
        itemSet itemset=new itemSet("all items", item.size(),item);
        return itemset;
    }
   
    @POST
    @Path("createItem")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces(MediaType.TEXT_PLAIN)
    
    public Long createItem(@FormParam("name") String name)
    { 
        //String name=formData.getFirst("name1");
        System.out.println("********* formParam" + name);               
       return request.createItem(name);
       
    }
    
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @POST
    @Path("createUser")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces(MediaType.TEXT_PLAIN)
    public String createUser(@FormParam("username") String username,@FormParam("country") String country, @FormParam("zipcode") String zipcode, @FormParam("bankaccount") String bankaccount,@FormParam("email")String email, @FormParam("password") String password){
          Long userId=request.createCustomer(username, country, zipcode, bankaccount, email, password);
          return userId.toString();
    }
    
    
    
    @POST
    @Path("identifyUser")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces(MediaType.TEXT_PLAIN)
    public Boolean identifyUser(@FormParam("email") String email ,@FormParam("password") String password)
    {
        Boolean boo=request.identifyCustomer(email, password);
        return boo;
    }
    
    @POST
    @Path("createShop")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces(MediaType.TEXT_PLAIN)
    public Long createShop(@FormParam("ownerId") Long ownerId,@FormParam("shopName") String shopName, @FormParam("shopDescription") String shopDescription)
    {
      return request.createShop(ownerId, shopName, shopDescription);
    }
    
    @POST
    @Path("createSell_FixedPrice")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces(MediaType.TEXT_PLAIN)
    public Long createSell_FixedPrice(@FormParam("shopId") Long shopId, @FormParam("itemId") Long itemId, Long price,Long storage)
    {
        return request.createSell_FixedPrice(shopId, itemId, price,storage);
    }
    
    @POST
    @Path("createSingleOrder")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces(MediaType.TEXT_PLAIN)
    public Long createOrder(Long ordersetId,Long sellId,Long quantity)
    {
       return request.createOrder(ordersetId, sellId, quantity);
    }
    
    @POST
    @Path("createOrderSet")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces(MediaType.TEXT_PLAIN)
    public Long createOrderSet(Long customerId, Long shopId)
    {
        return request.createOrderSet(customerId, shopId);
    }
    @POST
    @Path("getAllCurrentCustomers")
    @Consumes("application/xml")
    @Produces("application/xml")
    public customerSet getAllCurrentCustomers()
    {
        List<Customer> customers=request.showAllCustomer();
        customerSet customerset=new customerSet("All Customers",customers.size(),customers);
        System.out.println("Test ******* getAllCurrentCustomers "+customerset.getCount());
        return customerset;
    }
    @POST
    @Path("deleteUser")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces(MediaType.TEXT_PLAIN)
    public String deleteUser(@FormParam("id") Long id)
    {
       request.deleteCustomer(id);
       return "OK";
    }
}